﻿using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIEntityViewHelper : IReusableClass
    {
        private string assetPath;
        private UIEntityView entity;
        private bool isLoaded = false;
        private System.Action<UIEntityView> callSuccessLoaded;

        public UIEntityViewHelper Sync(System.Action<UIEntityView> funcSuccessLoaded)
        {
            this.callSuccessLoaded = funcSuccessLoaded;
            return this;
        }

        private void ExcuteSuccessCall()
        {
            this.callSuccessLoaded?.Invoke(this.entity);
        }

        public void ShowModel(string assetPath, GameObject gobjParent)
        {
            this.ShowModel(assetPath, gobjParent.transform);
        }

        public void ShowModel(string assetPath, Transform parent)
        {
            if (string.Equals(this.assetPath, assetPath))
            {
                if (this.isLoaded)
                {
                    this.ExcuteSuccessCall();
                }
                return;
            }
            this.ClearModel();
            if (parent == null)
            {
                return;
            }
            this.LoadModelAsync(assetPath, parent).Forget();
        }

        private async UniTaskVoid LoadModelAsync(string assetPath, Transform parent)
        {
            this.assetPath = assetPath;
            Entity baseEntity = ManagerCenter.Entity.Get(assetPath, (int)EntityPoolGroupType.UIEntity);
            var result = await baseEntity.Load();
            UIEntityView entity = (UIEntityView)baseEntity;
            if (result == EntityLoadState.Error)
            {
                LogGame.LogError($"UIEntityViewHelper LoadUIModelAsync Error Prefab is null,assetPath = {assetPath} , type = {entity.GetType()}");
                this.HideModel(entity);
                return;
            }
#if UNITY_EDITOR
            LogGame.Log($"=== UIEntityViewHelper LoadUIModelAsync : {baseEntity.Id}, {baseEntity.GetType()}, {baseEntity.gameObject}");
#endif
            this.OnLoadedUIModel(entity, assetPath, parent);
        }

        private void OnLoadedUIModel(UIEntityView entity, string assetPath, Transform parent)
        {
            if (parent == null)
            {
                this.assetPath = null;
                this.HideModel(entity);
                return;
            }
            if (string.Equals(assetPath, this.assetPath))
            {
                this.entity = entity;
                this.isLoaded = true;
                var transform = this.entity.transform;
                transform.SetParent(parent, false);
                this.ExcuteSuccessCall();
            }
            else
            {
                this.HideModel(entity);
            }
        }

        private void HideModel(UIEntityView entity)
        {
            if (entity == null)
            {
                return;
            }
#if UNITY_EDITOR
            LogGame.Log($"=== UIEntityViewHelper HideEntity : {entity.Id}, {entity.GetType()}, {entity.gameObject}");
#endif
            ManagerCenter.Entity.HideEntity(entity);
        }

        public void Clear()
        {
            this.ClearModel();
            this.callSuccessLoaded = null;
        }

        private void ClearModel()
        {
            this.assetPath = null;
            this.HideModel(this.entity);
            this.entity = null;
            this.isLoaded = false;
        }

        #region 对象池
        public uint MaxStore => 3;

        public void OnReset()
        {
            this.Clear();
        }

        public void Put2Pool()
        {
            ClassPool.PutAutoType(this);
        }

        static public UIEntityViewHelper NewFromPool()
        {
            return ClassPool<UIEntityViewHelper>.Get();
        }
        #endregion

    }
}
